#include<stdio.h>
class TreeNode:
    def __init__(self, val=0, left=None, right=None):
        self.val = val
        self.left = left
        self.right = right

def is_valid_bst(node, min_val=float('-inf'), max_val=float('inf')):
    if not node:
        return True
    if node.val <= min_val or node.val >= max_val:
        return False
    return (is_valid_bst(node.left, min_val, node.val) and 
            is_valid_bst(node.right, node.val, max_val))

def check_balance(node):
    if not node:
        return (0, True)
    left_height, left_balanced = check_balance(node.left)
    right_height, right_balanced = check_balance(node.right)
    is_balanced = left_balanced and right_balanced and (abs(left_height - right_height) <= 1)
    current_height = 1 + max(left_height, right_height)
    return (current_height, is_balanced)

def is_valid_avl(root):
    if not is_valid_bst(root):
        return False
    _, balanced = check_balance(root)
    return balanced
